package com.nebula.api.generator;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import link.ahsj.generator.utils.GeneratorUtils;
import org.junit.jupiter.api.Test;

/**
 * @author Eastern unbeaten
 * @version 1.0
 * @date 2019/8/18 22:40
 * @mail chenshiyun2011@163.com
 */
public class Demo {

    //D:\IdeaCode\nebula-web\api\src\test\java\com\nebula\api
    //D:\IdeaCode\nebula-web/src/test/java\com\nebula\api\xxxx\entity\Xxxx.java
    public static void main(String[] args) {
        GeneratorUtils.generator(
                "api",
                "com.nebula.api.modules",
                "member",
                DbType.MYSQL,
                "C:\\Users\\Administrator\\Desktop\\src",
                // 页面上的父上下文
                // 自己的上下文
                "goods",
                "test",
                null,
                "member",
                "member",
                "ChanlunLee",
                "127.0.0.1",
                "3306",
                "jifen-web?useSSL=false&serverTimezone=GMT%2B8&characterEncoding=utf8",
                "root",
                "123456",
                new String[]{"bmd_", "mp_", "SYS_", "TB_"},
                new String[]{"cmf_member"}, false
        );
    }

    @Test
    public void generate() {
        // 创建代码构造器
        AutoGenerator generator = new AutoGenerator();

        // 全局配置
        GlobalConfig config = new GlobalConfig();
        String projectPath = System.getProperty("user.dir");
        // 设置输出到的目录
        config.setOutputDir(projectPath + "/src/main/java");
        config.setAuthor("my");
        // 生成结束后是否打开文件夹
        config.setOpen(false);

        // 全局配置添加到 generator 上
        generator.setGlobalConfig(config);

        // 数据源配置
        DataSourceConfig dataSourceConfig = new DataSourceConfig();
        dataSourceConfig.setUrl("jdbc:mysql://localhost:3306/jifen-web?characterEncoding=utf-8&serverTimezone=UTC");
        dataSourceConfig.setDriverName("com.mysql.cj.jdbc.Driver");
        dataSourceConfig.setUsername("root");
        dataSourceConfig.setPassword("root");

        // 数据源配置添加到 generator
        generator.setDataSource(dataSourceConfig);

        // 包配置, 生成的代码放在哪个包下
        PackageConfig packageConfig = new PackageConfig();
        packageConfig.setParent("com.nebula.api.modules.product");

        // 包配置添加到 generator
        generator.setPackageInfo(packageConfig);

        // 策略配置
        StrategyConfig strategyConfig = new StrategyConfig();
        // 下划线驼峰命名转换
        strategyConfig.setNaming(NamingStrategy.underline_to_camel);
        strategyConfig.setColumnNaming(NamingStrategy.underline_to_camel);
        // 开启lombok
        strategyConfig.setEntityLombokModel(true);
        // 开启RestController
        strategyConfig.setRestControllerStyle(true);
        strategyConfig.setInclude("shop_product", "shop_product_base_info", "shop_product_column", "shop_product_column_list");
        generator.setStrategy(strategyConfig);
        generator.setTemplateEngine(new FreemarkerTemplateEngine());

        // 开始生成
        generator.execute();
    }


}
